package com.typing.battle.activity;

import org.json.JSONArray;
import org.json.JSONObject;

import com.typing.battle.R;
import com.typing.battle.activity.TypingBattleChatRoomInfoActivity.UserAdapter;
import com.typing.battle.listener.OnDataSyncCompleteListener;
import com.typing.battle.util.ServerComunicationUtil;
import com.typing.battle.value.ServerComunicationDTO;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class TypingBattleChatRoomInfoActivity extends TypingBattleRootActivity{

	private ListView vList;
	private UserAdapter adapter;
	private LayoutInflater inflate;
	private TextView vTitle;
	private TextView vUserInfo;
	private JSONArray memberList;
	private JSONObject roomInfo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.battle_chat_room_info);
		inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		vList =(ListView)findViewById(R.id.list_userid);
		vTitle = (TextView)findViewById(R.id.tv_room_title);
		vUserInfo = (TextView)findViewById(R.id.tv_room_user_info);
		getRoomInfo();
//		setData();
	}
	private void getRoomInfo(){
		ServerComunicationDTO dto = new ServerComunicationDTO();
		dto.setActionType(ServerComunicationUtil.TYPE_CHAT_ROOM_DETAIL);
		dto.setResponseType(ServerComunicationUtil.RETURN_TYPE_STRING);
		dto.setChatRoomId(getIntent().getStringExtra("chatRoomId"));
		
		try{
			ServerComunicationUtil.serverAction(dto,this,this);
		}catch(Exception e){
			Log.e("", "TYPE_CHAT_ROOM_DETAIL ERROR ::: "+e.getMessage());
			e.printStackTrace();
		}
	}
	private void setData(){
		//제목 셋팅
		try{
			
//			chatRoomOwner":"","chatRoomId":"1","chatRoomType":"0",
//			"orderByClause":"","userEmail":"","oredCriteria":[],"maxPeopleCount":"3","chatRoomName":"ㅠㅇㅍㄷ듀"}]
			String roomType ="0".equals(roomInfo.getString("chatRoomType"))?"[배틀]":"[일반]";
			vTitle.setText("No."+roomInfo.getString("chatRoomId")+" " + roomInfo.getString("chatRoomName"));
			//참여인원 셋팅 
			vUserInfo.setText("Type : "+roomType+" ,참여인원 : ("+memberList.length() + "/" + roomInfo.getString("maxPeopleCount") +")");
		}catch(Exception e){
			e.printStackTrace();
		}
		
		//아이디 리스트 셋팅 
		adapter = new UserAdapter();
		vList.setAdapter(adapter);
	}
	class UserAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return memberList==null ? 0 : memberList.length();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LinearLayout layout;
			if(convertView ==null){
				layout  = (LinearLayout)inflate.inflate(R.layout.chat_room_info_id_list_row, null);
			}else{
				layout =(LinearLayout)convertView;	
			}
			TextView userId = (TextView)layout.findViewById(R.id.info_user_id);
			TextView userSex = (TextView)layout.findViewById(R.id.info_user_sex);
			TextView userHighSpeed = (TextView)layout.findViewById(R.id.info_user_high_speed);
			TextView userAccuracy = (TextView)layout.findViewById(R.id.info_user_accuracy);
			try{
				JSONObject data = new JSONObject(memberList.get(position).toString());
				
				userId.setText(data.getString("name"));
				userHighSpeed.setText(data.getString("typing_speed")+" 타");
				userAccuracy.setText(data.getString("typing_accuracy")+" %");
				if("M".equals(data.getString("sex"))){
					userSex.setText("♂");
					userSex.setTextColor(Color.RED);
				}else{
					userSex.setText("♀");
					userSex.setTextColor(Color.BLUE);
				}
				
			}catch(Exception e){
				
			}
			return layout;
		}
		
	}
	
	@Override
	public void onSyncComplete(int resultCode, ServerComunicationDTO dto, Object resultObj) {
		if(resultCode == OnDataSyncCompleteListener.RESULT_SYNC_FAIL){
			if(resultObj!=null)
				ServerComunicationUtil.showNotify((String)resultObj, this);
		}else{
			if(dto.getActionType() == ServerComunicationUtil.TYPE_CHAT_ROOM_DETAIL){
				try{
					JSONObject resultObject = new JSONObject((String)resultObj);
					memberList = new JSONArray(resultObject.get("memberList").toString());
					roomInfo = new JSONObject(resultObject.get("roomInfo").toString());
					setData();
					adapter.notifyDataSetChanged();
				}catch(Exception e){
					e.printStackTrace();
				}
			}		
		}
		super.onSyncComplete(resultCode, dto, resultObj);
	}
}
